package com.example.electronicmall.base;

import androidx.annotation.LongDef;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.electronicmall.R;
import com.example.electronicmall.adapter.DocxAdapter;
import com.example.electronicmall.application.BaseApplication;
import com.example.electronicmall.bean.DocxFristBean;
import com.example.electronicmall.bean.DocxOmsCartItemListBean;
import com.example.electronicmall.bean.DocxSeconBean;
import com.example.electronicmall.bean.Docxbean;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DocxActivity extends AppCompatActivity {
    private final  String TAG=DocxActivity.class.getCanonicalName();
    private final  int REQUEST_CODE_REGIST=0xf1;
    private TextView name;
    private TextView position;
    private TextView phonenumber;
    private Docxbean docxbean;
    private List<Docxbean> docxbeanList;
    private DocxAdapter adapter;
    private TextView docxtotal;//
    private double tatolall;//商品合计
    private String s;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_docx);
        initview();
        goPayment();
    }
    private void initview() {
        docxtotal = findViewById(R.id.activity_docx_tatal);
        position = findViewById(R.id.activity_docx_position);
        name = findViewById(R.id.activity_docx_name);
        phonenumber = findViewById(R.id.activity_docx_phonenumber);
        ImageView choiceposition = findViewById(R.id.activity_docx_choiceposition);
        RecyclerView recyclerView = findViewById(R.id.docx_activivty_recycyview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //选择收货地址
        choiceposition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(DocxActivity.this, ChoicepositionActivity.class), CONTEXT_INCLUDE_CODE);
            }
        });
        int userId = 1;//用户ID
        int userDuctId = 1;//商品TD
        int[] userSku = {180, 98, 171, 179};//商品SKU
        for (int n = 0; n < userSku.length;n++) {
            BaseApplication.request.requestshoppingdocx(userId, userDuctId, userSku[n])
                    .enqueue(new Callback<DocxFristBean>() {
                        @Override
                        public void onResponse(Call<DocxFristBean> call, Response<DocxFristBean> response) {
                            //当前收货地址
                            String province = response.body().getData().getUmsMemberReceiveAddress().getProvince();
                            String city = response.body().getData().getUmsMemberReceiveAddress().getCity();
                            String region = response.body().getData().getUmsMemberReceiveAddress().getRegion();
                            String detailAddress = response.body().getData().getUmsMemberReceiveAddress().getDetailAddress();
                            position.setText(province + city + region + detailAddress);
                            name.setText(response.body().getData().getUmsMemberReceiveAddress().getName());
                            phonenumber.setText(response.body().getData().getUmsMemberReceiveAddress().getPhoneNumber());
                            //当前选择的商品
                            DocxOmsCartItemListBean[] data = response.body().getData().getOmsCartItemList();
                            docxbeanList = new ArrayList<>();
                            for (int i = 0; i < data.length; i++) {
                                docxbean = new Docxbean();
                                docxbean.setDocxname(data[i].getProductName());
                                docxbean.setSku(data[i].getProductSubTitle());
                                tatolall += data[i].getPrice();
                            }
                            //转化图片
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    for (int j = 0; j < data.length; j++) {
                                        ;
                                        try {
                                            Bitmap bitmap;
                                            URL uri = new URL(data[j].getProductPic());
                                            HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
                                            connection.setReadTimeout(5000);
                                            connection.setConnectTimeout(5000);
                                            connection.connect();
                                            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                                                InputStream inputStream = connection.getInputStream();
                                                bitmap = BitmapFactory.decodeStream(inputStream);
                                                Log.d(TAG, "转化图片" + bitmap);
                                                docxbean.setDocxicon(bitmap);
                                            }
                                        } catch (MalformedURLException e) {
                                            e.printStackTrace();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }

                                    }
                                }
                            }).start();
                            docxbeanList.add(docxbean);
                            adapter = new DocxAdapter(DocxActivity.this, docxbeanList);
                            recyclerView.setAdapter(adapter);
                            //商品合计价格
                            Log.d(TAG, "商品合计 " + tatolall);
                            s = String.valueOf(tatolall);
                            docxtotal.setText(s);
                        }

                        @Override
                        public void onFailure(Call<DocxFristBean> call, Throwable t) {
                            Log.d(TAG, "异常错误 " + t.fillInStackTrace());
                        }
                    });
        }
    }
    //接收选择的收货地址
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable  Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
          if (resultCode==RESULT_OK) {
              switch (requestCode) {
                  case REQUEST_CODE_REGIST:
                 String names =data.getStringExtra("name").trim();
                 String phonenumbers=data.getStringExtra("phone").trim();
                 String positions=data.getStringExtra("position").trim();
                     Log.d(TAG, "传过来的数据" + data);
                     Log.d(TAG, "传过来的名字" + names);
                     Log.d(TAG, "传过来的电话号" + phonenumbers);
                     Log.d(TAG, "传过来的名字" + positions);
                     name.setText(names);
                     phonenumber.setText(phonenumbers);
                     position.setText(positions);
                  break;
              }
          }
    }
    private void goPayment(){
        TextView payment=findViewById(R.id.activity_docx_ok);
        payment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(DocxActivity.this,PaymentActivity.class);
                intent.putExtra("money",s);
                startActivity(intent);
            }
        });
    }
}